package com.wang.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Classname SessionController
 * @Description TODO
 * @Date 2024/5/31 8:51
 * @Created by pc
 */
@RestController
@Slf4j
public class SessionController {

    @RequestMapping("/session")
    public Object springSession(String username, HttpServletRequest request, HttpSession session){
        Cookie[] cookies = request.getCookies();
        if(null != cookies && cookies.length > 0){
            for (Cookie cookie : cookies) {
                log.warn(cookie.getName() + " = " + cookie.getValue());
            }
        }
        Object value = session.getAttribute("username");
        if(null == value){
            log.warn("用户不存在");
            session.setAttribute("username","{username:'" + username + "',age: 30}");
        }else{
            log.warn("用户存在");
        }
        return "username" + value;
    }
}
